#include "actionprocess.h"
#include <QProcess>

ActionProcess::ActionProcess()
{
    classname = __FUNCTION__;
    actParam = QJsonObject();
}

ActionProcess::ActionProcess(QString fullPath,
                             const QStringList &param,
                             const QString &workingDirectory)
{
    classname = __FUNCTION__;
    actParam = QJsonObject();
    actParam.insert("path",fullPath);
    QJsonArray arr;
    foreach(QString a, param)
    {
        arr.push_back(a);
    }
    actParam.insert("param",arr);
    actParam.insert("work-dir",workingDirectory);
}

ExecuteRet ActionProcess::execute()
{
    QString fullPath = actParam.value("path").toString();
    QJsonArray params = actParam.value("param").toArray();
    QString workDir = actParam.value("work-dir").toString();
    QStringList s;
    foreach(QJsonValue v, params)
    {
        s << v.toString();
    }
    qInfo()<<classname<<"start process"<<fullPath<<"params"<<params;
    bool ret = QProcess::startDetached(fullPath, s, workDir);
    if(ret){
        return ERSucc;
    }
    else{
        message = "start process failed.";
        return ERError;
    }
}
